package com.example.teacherservice.controller;

import com.example.teacherservice.dto.PracticeDetailDTO;
import com.example.teacherservice.dto.PracticeSelectionDTO;
import com.example.teacherservice.dto.StudentPracticeDTO;
import com.example.teacherservice.service.PracticeService;
import com.example.teacherservice.service.PracticeStudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// PracticeStudentController.java
@RestController
@RequestMapping("/api/student/practices")
@RequiredArgsConstructor
public class PracticeStudentController {
    private final PracticeStudentService practiceStudentService;
    private final PracticeService practiceService;

    // 学生查看所有可加入的实践活动
    @GetMapping
    public ResponseEntity<List<PracticeSelectionDTO>> getAllPractices(
            @RequestParam Long studentId) {
        return ResponseEntity.ok(
                practiceStudentService.getAllPracticesForStudent(studentId)
        );
    }

    // 学生加入实践活动
    @PostMapping("/{practiceId}/join")
    public ResponseEntity<Void> joinPractice(
            @RequestParam Long studentId,
            @PathVariable Long practiceId) {
        practiceStudentService.joinPractice(studentId, practiceId);
        return ResponseEntity.ok().build();
    }

    // 学生退出实践活动
    @DeleteMapping("/{practiceId}/quit")
    public ResponseEntity<Void> quitPractice(
            @RequestParam Long studentId,
            @PathVariable Long practiceId) {
        practiceStudentService.quitPractice(studentId, practiceId);
        return ResponseEntity.noContent().build();
    }

    // 学生查看自己已加入的实践活动
    @GetMapping("/my-practices")
    public ResponseEntity<List<StudentPracticeDTO>> getMyPractices(
            @RequestParam Long studentId) {
        return ResponseEntity.ok(
                practiceStudentService.getJoinedPractices(studentId)
        );
    }

    // 获取实践详情
    @GetMapping("/{practiceId}")  // 保持与前端调用一致
    public ResponseEntity<PracticeDetailDTO> getPracticeDetail(
            @PathVariable("practiceId") Long practiceId) {
        PracticeDetailDTO detail = practiceService.getPracticeDetailAlternative(practiceId);
        return ResponseEntity.ok(detail);
    }

}
